package com.zl.autocode.service.autocode.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeEnum;
import com.zl.autocode.dao.autocode.ZlTemplateDtlMapper;
import com.zl.autocode.dto.autocode.templateDtl.ReqTemplateDtlAddDTO;
import com.zl.autocode.dto.autocode.templateDtl.ReqTemplateDtlDelDTO;
import com.zl.autocode.dto.autocode.templateDtl.ReqTemplateDtlDetailDTO;
import com.zl.autocode.dto.autocode.templateDtl.ReqTemplateDtlEditDTO;
import com.zl.autocode.dto.autocode.templateDtl.ReqTemplateDtlQueryDTO;
import com.zl.autocode.dto.autocode.templateDtl.ReqTemplateDtlStateDTO;
import com.zl.autocode.dto.autocode.templateDtl.ResTemplateDtlAddDTO;
import com.zl.autocode.dto.autocode.templateDtl.ResTemplateDtlDelDTO;
import com.zl.autocode.dto.autocode.templateDtl.ResTemplateDtlDetailDTO;
import com.zl.autocode.dto.autocode.templateDtl.ResTemplateDtlEditDTO;
import com.zl.autocode.dto.autocode.templateDtl.ResTemplateDtlQueryDTO;
import com.zl.autocode.dto.autocode.templateDtl.ResTemplateDtlStateDTO;
import com.zl.autocode.dto.autocode.templateDtl.ResTemplateDtlVO2;
import com.zl.autocode.entity.autocode.ZlTemplateDtl;
import com.zl.autocode.entity.autocode.po.ZlTemplateDtlVo;
import com.zl.autocode.service.autocode.ITemplateDtlService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 模板明细
 * @author ZL
 */
@Service("autocodeTemplateDtlService")
public class TemplateDtlServiceImpl extends AutocodeBaseManager implements ITemplateDtlService {

    @Autowired
    private ZlTemplateDtlMapper templateDtlMapper;

    @Override
    public ResTemplateDtlQueryDTO queryTemplateDtlList(ReqDTO<ReqTemplateDtlQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTemplateDtlQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResTemplateDtlQueryDTO resDTO = BeanUtil.copyProperties(page, ResTemplateDtlQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getFileTitle())){
            reqData.setFileTitle("%"+reqData.getFileTitle()+"%");
        }
        if(StringUtils.isNotBlank(reqData.getFileName())){
            reqData.setFileName("%"+reqData.getFileName()+"%");
        }
        if(StringUtils.isNotBlank(reqData.getQuoteTag())){
            reqData.setQuoteTag("%"+reqData.getQuoteTag()+"%");
        }
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData,false,true);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlTemplateDtlVo> list = templateDtlMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResTemplateDtlVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResTemplateDtlDetailDTO queryTemplateDtlDetail(ReqDTO<ReqTemplateDtlDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTemplateDtlDetailDTO reqData = reqInfo.getReqData();
        ResTemplateDtlDetailDTO res = new ResTemplateDtlDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZlTemplateDtl view = templateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResTemplateDtlAddDTO insertTemplateDtlInfo(ReqDTO<ReqTemplateDtlAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTemplateDtlAddDTO reqData = reqInfo.getReqData();
        ResTemplateDtlAddDTO res = new ResTemplateDtlAddDTO();
        Date time = getSystemTime().getDate();
        
        ZlTemplateDtl record = new ZlTemplateDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setTemplateFileId(IdUtil.simpleUUID());
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateFileId",record.getTemplateFileId());
        //检查主键是否冲突
        if(templateDtlMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        templateDtlMapper.insertSelective(record);
        
        ZlTemplateDtl keyInfo = templateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResTemplateDtlEditDTO updateTemplateDtlInfo(ReqDTO<ReqTemplateDtlEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTemplateDtlEditDTO reqData = reqInfo.getReqData();
        ResTemplateDtlEditDTO res = new ResTemplateDtlEditDTO();
        Date time = getSystemTime().getDate();
        
        ZlTemplateDtl record = new ZlTemplateDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        templateDtlMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateFileId",record.getTemplateFileId());
        ZlTemplateDtl keyInfo = templateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResTemplateDtlDelDTO deleteTemplateDtlInfo(ReqDTO<ReqTemplateDtlDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTemplateDtlDelDTO reqData = reqInfo.getReqData();
        ResTemplateDtlDelDTO res = new ResTemplateDtlDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        templateDtlMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResTemplateDtlStateDTO updateTemplateDtlState(ReqDTO<ReqTemplateDtlStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqTemplateDtlStateDTO reqData = reqInfo.getReqData();
        ResTemplateDtlStateDTO res = new ResTemplateDtlStateDTO();
        Date time = getSystemTime().getDate();
         
        ZlTemplateDtl record = new ZlTemplateDtl();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        templateDtlMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateFileId",record.getTemplateFileId());
        ZlTemplateDtl keyInfo = templateDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}